// Package global
// @Author: zhangdi
// @File: func
// @Version: 1.0.0
// @Date: 2023/5/30 18:00
package entry

import (
	"github.com/wailsapp/wails/v2/pkg/runtime"
	"ideatools/application/constant"
	"ideatools/application/constant/enums"
	ctx2 "ideatools/application/core/ctx"
	"ideatools/application/core/events"
	"ideatools/application/pkgs/clips"
	"ideatools/application/pkgs/filehelper"
	"ideatools/application/pkgs/mylog"
	"ideatools/application/pkgs/utils"
	"path/filepath"
)

var listConfigKey []string = []string{
	constant.ConfigKeyHotWinQuick,
	constant.ConfigKeyHotWinMain,
	constant.ConfigKeyHotWinChat,
	constant.ConfigKeyHotNewChat,
	constant.ConfigKeyHotNewChatSelect,
}

func ShowOrHideApp() {
	ctx := ctx2.GetCtx()
	clips.CopySelect()
	//切换到快捷弹出面板
	events.OnChangeWindow(enums.WindowsTypeQuickTools)

	if runtime.WindowIsMinimised(ctx) {
		runtime.WindowUnminimise(ctx) //恢复正常
		//runtime.WindowMinimise(ctx) //最小化
	}
}

// HideApp 隐藏
func HideApp() {
	ctx := ctx2.GetCtx()
	runtime.WindowMinimise(ctx) //最小化
}

func NotifyKeyBoardEvent(codes []uint16) {
	ctx := ctx2.GetCtx()
	runtime.EventsEmit(ctx, "keyboard_combine_click", codes)
}

func NotifyMouseLongClick(code uint16) {
	ctx := ctx2.GetCtx()
	runtime.EventsEmit(ctx, "mouse_long_click", code)
}

func HotKeyIsUse(combineKey string) bool {
	//1.检测[配置中的]启动快捷键
	if CheckConfigHotKeyIsUse(combineKey) {
		return true
	}

	//2.检测应用快捷键
	info, _ := UserToolService.GetUserToolByHotKey(combineKey)
	if info != nil {
		return true
	}

	return false
}

func CheckConfigHotKeyIsUse(combineKey string) bool {

	for _, k := range listConfigKey {
		QuickerOpenHotKey := ConfigService.Get(k)
		if QuickerOpenHotKey == combineKey {
			return true
		}
	}

	return false
}

func IsFirstStart(ProjectName string) bool {
	firstCheckPath := filepath.Join(utils.GetUserHomeDir(), ProjectName, "first.lock")
	mylog.Info("lock 文件" + firstCheckPath)
	if !filehelper.FileExists(firstCheckPath) {
		filehelper.CreateFile(firstCheckPath)
		return false
	}
	return true
}
